/**
 * @author Richie
 * @constructor 
 */
OtherUtils = function () {
	/**
	 * <b>限制身份證號輸入</b><br/>
	 * 使用方法：onkeyup = "OtherUtil.setDateConstraint(this)"
	 * @param	obj 使用物件
	 * @return	{String}
	 */
	this.setIdNumberConstraint = function (obj) {
		var value = obj.value;
		if (value.length == 1) {
			obj.value = obj.value.replace(/[^a-zA-Z]/g,'').toUpperCase();
		} else if(value.length > 1) {
			var temp1 = value.substring(0, 1);
			var temp2 = value.substring(1,2);
			var temp3 = value.substring(2);
			temp1 = temp1.replace(/[^a-zA-Z]/g,'');
			temp2 = temp2.replace(/[^1-2]/g,'');
			temp3 = temp3.replace(/[^0-9]/g,'');
			obj.value = temp1.toUpperCase() + temp2 + temp3;
		}
	};

	/**
	 * <b>判斷身分證字號是否正確</b>
	 * @param 	checkStr 欲判斷之字串
	 * @return 	{boolean}
	 */
	this.isIdNumber = function (checkStr) {
		var result = true;
		if(checkStr != ""){
			// 依照字母的編號排列，存入陣列備用。
			var letters = new Array('A', 'B', 'C', 'D',
					'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
					'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
					'X', 'Y', 'W', 'Z', 'I', 'O');
			// 儲存各個乘數
			var multiply = new Array(1, 9, 8, 7, 6, 5, 4, 3, 2, 1);
			var nums = new Array(2);
			var firstChar;
			var firstNum;
			var lastNum;
			var total = 0;
			// 撰寫「正規表達式」。第一個字為英文字母，
			// 第二個字為1或2，後面跟著8個數字，不分大小寫。
			var regExpID = /^[a-z](1|2)\d{8}$/i;
			// 使用「正規表達式」檢驗格式
			if (checkStr.search(regExpID) == -1) {
				result = false;
			} else {
				// 取出第一個字元和最後一個數字。
				firstChar = checkStr.charAt(0).toUpperCase();
				lastNum = checkStr.charAt(9);
			}
			// 找出第一個字母對應的數字，並轉換成兩位數數字。
			for (var i=0; i<26; i++) {
				if (firstChar == letters[i]) {
					firstNum = i + 10;
					nums[0] = Math.floor(firstNum / 10);
					nums[1] = firstNum - (nums[0] * 10);
					break;
				}
			}
			// 執行加總計算
			for (var i=0; i < multiply.length; i++) {
				if (i < 2) {
					total += nums[i] * multiply[i];
				} else {
					total += parseInt(checkStr.charAt( i- 1)) * multiply[i];
				}
			}
			//規則一餘數為零，且檢查碼需為零
			if (lastNum == 0 && (total % 10) != lastNum ){
				result = false;
			}
			//規則二餘數與檢查碼需相符
			if (lastNum != 0 && (10 - (total % 10))!= lastNum) {
				result = false;
			}
		}
		return result;
	};
	
	/**
	 * <b>設立警示欄位</b>
	 * @param 	obj 欲警示之欄位
	 * @param 	alertFlag 是否開啟警示
	 */
	this.setAlertField = function (obj, alertFlag){
		this.setFlashAlertField(obj, 0, alertFlag);
	};
	
	/**
	 * <b>設立閃爍警示欄位</b>
	 * @param 	obj 欲警示之物件欄位
	 * @param 	count 閃爍警示之次數
	 * @param 	alertFlag 閃爍警示完畢後是否開啟警示
	 * 
	 * 注意：下拉式選單尚不接受閃爍之型態
	 */
	this.setFlashAlertField = function (obj, count, alertFlag){
		if(count!=0){
			//firefox 不支援
//			if(alertFlag){
//				count--;
//				obj.style.background="#e8f3ff";
//				window.setTimeout(EBMSUtil.setFlashAlertField, 100, obj, count, false); 
//			}else{
//				obj.style.background="#ffffff";
//				window.setTimeout(EBMSUtil.setFlashAlertField, 100, obj, count, true); 
//			}
		}else{
			if(alertFlag){
				obj.style.background="#e8f3ff";
			}else{
				obj.style.background="#ffffff";
			}
		}
	};
	
	this.getElementsByNameToIEFirefox = function (tag, name) {
	    var elem = document.getElementsByTagName(tag);
	    var arr = new Array();
	    for(i = 0,iarr = 0; i < elem.length; i++) {
	         att = elem[i].getAttribute("name");
	         if(att == name) {
	              arr[iarr] = elem[i];
	              iarr++;
	         }
	    }
	    return arr;
	};
	
	
	//分析url
	this.parseURL = function parseURL(url) {
		var a = document.createElement('a');
		a.href = url;
		return {
			source: url,
			protocol: a.protocol.replace(':', ''),
			host: a.hostname,
			port: a.port,
			query: a.search,
			params: (function () {
						var ret = {},
						seg = a.search.replace(/^\?/, '').split('&'),
						len = seg.length, i = 0, s;
						for (; i < len; i++) {
							if (!seg[i]) { continue; }
							s = seg[i].split('=');
							ret[s[0]] = s[1];
						}
						return ret;
					})(),
			file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
			//hash: a.hash.replace('#', ''),
			hash: (function () {
						var ret = {},
						seg = a.hash.replace(/^\#/, '').split('&'),
						len = seg.length, i = 0, s;
						for (; i < len; i++) {
							if (!seg[i]) { continue; }
							s = seg[i].split('=');
							ret[s[0]] = s[1];
						}
						return ret;
					})(),
			path: a.pathname.replace(/^([^\/])/, '/$1'),
			relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
			segments: a.pathname.replace(/^\//, '').split('/'),
			serverUrl:"http://"+a.hostname+":"+a.port
		};
	};

	this.replaceUrlParams = function(myUrl, newParams, newParamsValue){
		var hasInMyUrlParams = false;
		for (var x in myUrl.haha) {
			if (newParams.toLowerCase() == x.toLowerCase()) {
				myUrl.hash[x] = newParamsValue;
				hasInMyUrlParams = true;
				break;
			}
		}
		if (!hasInMyUrlParams) {
			myUrl.hash[newParams] = newParamsValue;
		}
		
		var _result = myUrl.protocol + "://" + myUrl.host + ":" + myUrl.port + myUrl.path ;

		if (myUrl.query != "") {
			_result +=  myUrl.query;
		}
		
		if (myUrl.hash != "") {
			_result += "#" ;
		}
		
		for (var p in myUrl.hash) {
			_result += (p + "=" + myUrl.hash[p] + "&");
		}

		if (_result.substr(_result.length - 1) == "&") {
			_result = _result.substr(0, _result.length - 1);
		}
		

		//if (myUrl.hash != "") {
		//	_result += "#" + myUrl.hash;
		//}
		
		return _result;
	};


	this.setUrlHash = function(urlParams, urlParamsValue){
		var myURL = this.parseURL(String(location));
		var _newUrl = this.replaceUrlParams(myURL, urlParams, urlParamsValue );
		location =_newUrl;
	};
	
	this.getUrlHash = function(hashName){
		var returnValue = "";
		var url = String(location);
		if(url.indexOf(hashName)>0){
			var myUrl = EBMSUtil.parseURL(url);
			for (var x in myUrl.hash) {
				if (hashName == x) {
					returnValue = myUrl.hash[x] ;
					break;
				}
			}
		}
		return returnValue;
	};
	
	//為了Firefox可取得keyCode
	this.whichKeyPress = function(e) {
		if (!e) {
			//if the browser did not pass the event 
			//information to the function, 
			//we will have to obtain it from the 
			//event register
			if (window.event) {
				//Internet Explorer
				e = window.event;
			} else {
				//total failure, we have no 
				//way of referencing the event
				return;
			}
		}
		if (typeof (e.keyCode) == 'number') {
			//DOM
			e = e.keyCode;
			return e;
		} else if (typeof (e.which) == 'number') {
			//NS 4 compatible
			e = e.which;
			return e;
		} else if (typeof (e.charCode) == 'number') {
			//also NS 6+, Mozilla 0.9+
			e = e.charCode;
			return e;
		} else {
			//total failure, we have no way of obtaining the key code
			return;
		}
	};
}

var OtherUtils = new OtherUtils();